<script type="text/javascript" src="<?=base_url().'resource/js/layer/layer.js'?>" charset="utf-8"></script>
<script type="text/javascript">
    //弹出用户修改框
    function setWhsPermission(id)
    {
        var title='';
        title="仓库权限设置";
        layer.open({
            type: 1,
            title: title,
            area: ['540px', '500px'], //宽高
            content: $('#whspermitDiv')
        });
        $('#whspermitForm').attr('action',"<?=blang_url('wms/AuxFunction/setWhsPermission')?>");
        var index=layer.load(2);
        $.post("<?=blang_url('ajax/wmsajax/getUserWhsInfo')?>",{'user_id':id},function(data)
        {
            if(data)
            {
                var whsData=JSON.parse(data);
                var arr;
                var pointJson = {'W001':'sz','W005':'es','W006':'ru','W014':'de','W033':'gb','W032':'us','W040':'fr'};
                // console.log(whsData);
                layer.close(index);
                $('#user_id').val(id);
                $(".cbkLast").removeAttr('checked');
                $(".cbkSecond").removeAttr('checked');
                $(".cbkRoot").removeAttr('checked');
                $('ul[id^="ul_"]').attr('style','display:none');
                for (var i = 0; i < whsData.length; i++) 
                {
                    $("input[value='"+whsData[i].whs_code+"']:checkbox").prop('checked',true);
                    arr = whsData[i].whs_code.split('-');
                    point = arr[0];
                    type = arr[1];
                    if(type.indexOf('T') >= 0)
                    {
                        type = 'maintain';
                    }else
                    {
                        type = 'merchant';
                    }
                    $("input[value='"+whsData[i].whs_code+"']:checkbox").parent().parent().parent().parent().find('input[id="'+pointJson[point]+'_'+type+'"]').prop("checked", true);
                    // console.log('input[id="'+pointJson[point]+'_'+type+'"]');
                    $("input[value='"+whsData[i].whs_code+"']:checkbox").parent().parent().parent().parent().parent().parent().find('.cbkRoot').prop("checked", true);
                     $('ul[id^="ul_'+pointJson[point]+'"]').attr('style','display:block');
                }
            }
        });
    }
</script>
<style type="text/css">
    ul{
        list-style-type:none;
    }
    li{
        list-style-type:none;
    }
</style>
<!--top结束-->
<div class="mian">
    <div class="mian1">
        <div class="mian2">
            <!--left start-->
            <?php $this->load->view('defaults/common/left'); ?>
            <!--left end-->
            <!--right start-->
            <div class="right">
                <!--breadcrumb start-->
                <div class="righta"><?= $this->breadcrumb->generateHTML(); ?> </div>
                <!--breadcrumb end-->

                <!-- contents start -->
                <div id="contents">
                    <div id="ct_search">
                        <form action="<?=blang_url('wms/'.$class.'/'.$func.'/')?>" method="post" name="searchform"  >
                            <table id="search_tb">
                                <tr>
                                    <label>用户账号：
                                      <input type="text" name='account' value="<?=isset($account)?$account:'';?>"/>&nbsp;
                                    </label>
                                    <label>真实姓名：
                                        <input type="text" name='name' value="<?=isset($name)?$name:'';?>"/>&nbsp;</label>
                                        <input type="submit" name="searchSubmit" value="<?=lang('public_cx')?> " />
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                    <div class="ct_content">
                        <table class="tbwd800">
                            <tr>
                                <th width="6%">序号</th>
                                <th width="12%">用户账号</th>
                                <th width="15%">真实姓名</th>
                                <th width="20%">角色</th>
                                <th width="15%">邮箱</th>
                                <th width="12%">操作</th>
                            </tr>
                            <?php foreach ($dataList as $key => $row):?>
                                <tr>
                                    <td align="center"><?=$key + 1?></td>
                                    <td><?=$row['account']?></td>
                                    <td><?=$row['name']?></td>
                                    <td><?=$row['role_name']?></td>
                                    <td><?=$row['email']?></td>
                                    <td align="center"><button onclick="setWhsPermission(<?=$row['id']?>)">权限设置</button></td>
                                </tr>
                            <?php endforeach?>
                            <?php if(isset($pageShow) and !empty($pageShow)):?>
                                <tr align="center">
                                    <td colspan="6"><?=$pageShow?></td>
                                </tr>
                            <?php endif?>
                        </table>
                    </div>
                </div>
                <!-- contents end -->
            </div>
            <!--right end-->
            <div class="clear"></div>
        </div>
    </div>
</div>

<div class="ct_content" style="display: none;margin-left: 20px;margin-top: 10px;" id="whspermitDiv">
    <form action="" method="post" id="whspermitForm">
        <table cellspacing="0" style="border-collapse: collapse;width: 500px;">
            <tr>
                <th width="25%">权限选择：</th>
                <td>
                    <div style="margin-left: 30px;margin-top: 7px;">
                    <ul>
                        <?php foreach ($whsArr as $key => $value):?>
                            <li>
                                <label><input  type="checkbox" name='<?=$key?>' class="cbkRoot" id="<?=$key?>"/><?=$key?></label>
                                <ul style='display:none' id="<?='ul_'.$key?>">
                                    <?php foreach ($value as $k => $val):?>
                                        <li>
                                            <?=str_repeat('&nbsp;',4)?><label><input  type="checkbox" name='<?=$k?>' class='cbkSecond' id="<?=$key.'_'.$k?>" /><?=($k=='maintain')?'维保仓':'代管仓';?></label>
                                            <ul style='display:none' id="<?='ul_'.$key.$k?>">
                                                <?php foreach ($val as $k1 => $v):?>
                                                    <li>
                                                        <?=str_repeat('&nbsp;',8)?><label><input  type="checkbox" name='whs_code[]' value="<?=$k1?>" class='cbkLast' id="<?=$key.'_'.$k.'#'.$k1?>"/><?=$v?></label>
                                                    </li>
                                                <?php endforeach?>
                                            </ul>
                                        </li>
                                    <?php endforeach?>
                                </ul>
                            </li>
                        <?php endforeach?>
                    </ul>
                    </div>
                </td>
            <tr align="center">
                <td colspan="2">
                    <input type="hidden" name='user_id' id='user_id' />
                    <input type="submit"  value="<?=lang('public_tj')?>">
                </td>
            </tr>
        </table>
    </form>
</div>
<div style="clear:both"></div>
<script type="text/javascript">
    //父级的菜单选中，默认子类全部选中
    $('.cbkRoot').click(function () 
    {
        var id = $(this).attr('id');
        // console.log(id);
        // console.log($(this).is(':checked'));
        if ($(this).is(':checked')) 
        {
            $(this).parents('table').find('input[id^="'+id+'_"]').prop("checked", true);
            $(this).parents('table').find('ul[id^="ul_'+id+'"]').attr('style','display:block');
        } else {
            $(this).parent().parent().parent().find('input[id^="'+id+'_"]').prop("checked", false);
            $(this).parents('table').find('ul[id^="ul_'+id+'"]').attr('style','display:none');
        }
    });

    //第二级别的菜单选中，则默认最低级别的菜单全选
    $('.cbkSecond').click(function () 
    {
        var id = $(this).attr('id');
        id =id.substring(0,id.lastIndexOf("_"));
        console.log(id);
        if ($(this).is(':checked')) 
        {
            $(this).parent().parent().find('.cbkLast').prop("checked", true);
            //下面的代码是控制，第二级选中，第一级强制被选中
            $(this).parent().parent().parent().parent().find('input[id="'+id+'"]').prop("checked", true);
        } else 
        {
            $(this).parent().parent().find('.cbkLast').prop("checked", false);
        }

        //判断当前的table二级菜单有没有全部被选中
        var cnt_sec_chked = 0;
        $(this).parent().parent().parent().find('.cbkSecond').each(function () 
        {
            if ($(this).is(':checked')) 
            {
                cnt_sec_chked = cnt_sec_chked + 1;
            }
        });

        if (cnt_sec_chked == 0) 
        {
            $(this).parent().parent().parent().find('.cbkRoot').prop("checked", false);
        }
    });

    $('.cbkLast').click(function () 
    {
        var id = $(this).attr('id');
        id =id.substring(0,id.lastIndexOf("#"));
        // console.log(id);
        if ($(this).is(':checked')) 
        {
            $(this).parent().parent().parent().parent().find('input[id="'+id+'"]').prop("checked", true);
            //下面的代码是控制，第二级选中，第一级强制被选中
            $(this).parent().parent().parent().parent().parent().parent().find('.cbkRoot').prop("checked", true);

        }
    });
</script>